/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insta.calendarMicroBlogage;

import controlP5.ControlP5;
import g4p_controls.*;
import g4p_controls.GLabel;
 // Utilisation du paquetage video Movie myMovie;
//import codeanticode.gsvideo.*;
import processing.core.PApplet;
import processing.core.PImage;
import remixlab.proscene.*;
/**
 *
 * @author liban
 */
public class MySketch extends PApplet {

    /**
     * Demonstration of image buttons available in the G4P (GUI for Processing)
     * library. The button face comes from user supplied image files.
     *
     * (c) 2012 Peter Lager
     */

   // Variable
    GButton btnMakeWindow;
    GWindow window;
    //GSMovie movie;
    ControlP5 P5;
    boolean isOver = false;
    Scene scene;
    int num = 3;
    CreateScene _CurrentScene;
    Blogage facebook = new Blogage(0, 0, "data/facebook-logo.jpg", "data/facebook-logo.jpg");
    Blogage twitter = new Blogage(0, 0, "data/twitter.png", "data/twitter.png");
    Blogage instagram = new Blogage(0, 0, "data/instagram.jpg", "data/instagram.jpg");
    
    SceneFacebook maViewFacebook = new SceneFacebook("Facebook");
    SceneInstagram maViewInstagram = new SceneInstagram("Instagram");
    SceneTwitter maViewTwitter = new SceneTwitter("Twitter");
    SphereImage SphereConnected = new SphereImage();
    
    int ptsW, ptsH;
    GLabel label1;
    int value = 255;
    String renderer = P3D;
// SETUP
    private boolean CloseInstagramWindow = false;
    private boolean CloseFacebookWindow = false;
    private boolean CloseTwitterWindow = false;
    public void setup() {
        
        P5 = new ControlP5(this);
   //     movie = new GSMovie(this, "/Users/liban/Desktop/Processing/Image/Video.mov");
      //  movie.loop();
        size(600, 600, renderer);
        PImage image = loadImage("data/reseaux_sociaux.jpg");  
        image.resize(600, 600);
      background(image);
        
        sphereDetail(60);
        smooth();
        noStroke();
        //createLabels();
        ptsW = 20;
        ptsH = 20;
        SphereConnected.initializeSphere(ptsW, ptsH);

    }

    public void draw() {
        pushMatrix();
        pushStyle();
        translate(width / 2, height / 2, 0);
        translate(-150, 0, 0);
        rotateY(frameCount / 50f);
        textureSphere(50, 50, 50, instagram.getImage());
        rotateY(-frameCount / 50f);
        popStyle();
        popMatrix();

        pushMatrix();
        pushStyle();
        translate(width / 2, height / 2, 0);
        translate(0, 150, 0);
        rotateY(frameCount / 50f);
        textureSphere(50, 50, 50, facebook.getImage());
        rotateY(-frameCount / 50f);
        popStyle();
        popMatrix();

        pushMatrix();
        pushStyle();
        translate(width / 2, height / 2, 0);
        translate(150, 0, 0);
        rotateY(frameCount / 50f);
        textureSphere(50, 50, 50, twitter.getImage());
        rotateY(-frameCount / 50f);
        popStyle();
        popMatrix();
        
    }

   

    public void createLabels() {
        label1 = new GLabel(this, 340, 10, 110, 150);
        label1.setText("Choose your Blog");
        label1.setTextBold();
    }
    // creation des spheres

    public void textureSphere(float rx, float ry, float rz, PImage t) {
        // These are so we can map certain parts of the image on to the shape 
        int numPointsW = SphereConnected.getNumPointsW();
        int numPointsH = SphereConnected.getNumPointsH();

        float changeU = t.width / (float) (numPointsW - 1);
        float changeV = t.height / (float) (numPointsH - 1);
        float u = 0;  // Width variable for the texture
        float v = 0;  // Height variable for the texture
        beginShape(TRIANGLE_STRIP);
        texture(t);
        for (int i = 0; i < (numPointsH - 1); i++) {  // For all the rings but top and bottom
            // Goes into the array here instead of loop to save time
            float coory = SphereConnected.getCoorY()[i];
            float cooryPlus = SphereConnected.getCoorY()[i + 1];

            float multxz = SphereConnected.getMultXZ()[i];
            float multxzPlus = SphereConnected.getMultXZ()[i + 1];

            for (int j = 0; j < numPointsW; j++) {  // For all the pts in the ring
                normal(SphereConnected.getCoorX()[j] * multxz, coory, SphereConnected.getCoorZ()[j] * multxz);
                vertex(SphereConnected.getCoorX()[j] * multxz * rx, coory * ry, SphereConnected.getCoorZ()[j] * multxz * rz, u, v);
                normal(SphereConnected.getCoorX()[j] * multxzPlus, cooryPlus, SphereConnected.getCoorZ()[j] * multxzPlus);
                vertex(SphereConnected.getCoorX()[j] * multxzPlus * rx, cooryPlus * ry, SphereConnected.getCoorZ()[j] * multxzPlus * rz, u, v + changeV);
                u += changeU;
            }
            v += changeV;
            u = 0;
        }
        endShape();
    }

    public void MyDraw(Blogage MonBlog, int TranslateX, int TranslateY) {

        smooth();
        //scene.beginScreenDrawing();
        pushMatrix();
        // translate istagram
        pushMatrix();
        pushStyle();
        translate(width / 2, height / 2, 0);
        translate(TranslateX, TranslateY, 0);
      //fill(value);
        //rect(-150,-50, 100, 100);
        rotateY(-frameCount / 50f);
        //instagram.getImage().resize(0, 50);
        textureSphere(50, 50, 50, MonBlog.getImage());
      //texture(instagram.getImage());
        //image(facebook.getImage(),0,0);
        //SphereConnected.textureDeSphere(50, 50, 50, instagram);
        rotateY(-frameCount / 50f);
        //instagram.createLabels();
        pushStyle();
        popMatrix();

        // translate Facebook
        pushMatrix();
        translate(width / 2, height / 2, 0);
        translate(0, 150, 0);
        rotateY(-frameCount / 50f);
        //facebook.getImage().resize(0, 50);
        textureSphere(50, 50, 50, facebook.getImage());
      //SphereConnected.textureDeSphere(50, 50, 50, facebook.getImage());
        //image(facebook.getImage(),0,0);
        rotateY(-frameCount / 50f);
        popMatrix();

        // translate Twitter
        pushMatrix();
        translate(width / 2, height / 2, 0);
        translate(150, 0, 0);
        rotateY(-frameCount / 50f);
        //twitter.getImage().resize(0, 50);
        textureSphere(50, 50, 50, twitter.getImage());
      //SphereConnected.textureDeSphere(50, 50, 50, twitter.getImage());
        // image(twitter.getImage(),0,0);
        rotateY(-frameCount / 50f);
        popMatrix();

        popStyle();

        popMatrix();
     // 

    }

    public void mousePressed() {

    }

    public void mouseReleased() {
        
        float instagramXpos = 150;
        float instagramYpos = 300;
        float twitterXpos = 450;
        float twitterYpos = 300;
        float facebookXpos = 300;
        float facebookYpos = 450;
        float distX_Instagram_Mouse = instagramXpos - mouseX;
        float distY_Instagram_Mouse = instagramYpos - mouseY;   

        if (sqrt(sq(distX_Instagram_Mouse) + sq(distY_Instagram_Mouse)) < 50 && maViewInstagram.isCloseWindow() == false) {
            //createWindows("Instagram");
            maViewInstagram.createWindows(maViewInstagram.getMyname(), maViewInstagram.isCloseWindow());  
        }
         if (sqrt(sq(facebookXpos - mouseX) + sq(facebookYpos - mouseY)) < 50 && maViewFacebook.isCloseWindow() == false) {
             
            maViewFacebook.createWindows(maViewInstagram.getMyname(), maViewInstagram.isCloseWindow()); 
        }
        if (sqrt(sq(twitterXpos - mouseX) + sq(twitterYpos - mouseY)) < 50 && maViewTwitter.isCloseWindow() == false) {
            maViewTwitter.createWindows(maViewInstagram.getMyname(), maViewInstagram.isCloseWindow()); 
        }
       }
}
